package com.mvc.service.impl;

import java.util.ArrayList;
import java.util.Date;

import com.jfinal.plugin.activerecord.Page;
import com.mvc.model.Car;
import com.mvc.request.ReqCarAdd;
import com.mvc.request.ReqCarDelete;
import com.mvc.request.ReqCarPage;
import com.mvc.request.ReqCarQPage;
import com.mvc.request.ReturnData;
import com.mvc.response.RspPage;
import com.mvc.service.CarService;

/***
 * 样式风格
 * 
 * @author Swer
 * 
 */
public class CarServiceImpl implements CarService {

	public ReturnData<Boolean> add(ReqCarAdd req) {
		if (req == null) {
			return new ReturnData<Boolean>(false, 202, "error");
		}

		if (req.getId() <=0)
		{
			Car car = new Car();
			car.set(Car.TableField.CONTENT, req.getContent());
			car.set(Car.TableField.CREATETIME, new Date());
			car.set(Car.TableField.MAC, req.getFinalData().getMac());
			car.set(Car.TableField.PHONE, req.getPhone());
			car.set(Car.TableField.TYPE, req.getType());

			car.set(Car.TableField.FATIME,  req.getFaTime());
			car.set(Car.TableField.STARTADDRESS,  req.getStartAddress());
			car.set(Car.TableField.ENDADDRESS,  req.getEndAddress());
			car.set(Car.TableField.MONEY, req.getMoney());
			car.set(Car.TableField.SIZEPERSON, req.getSizePerson());
			
			if (car.save()) {
				return new ReturnData<Boolean>(true, 200, "OK");
			} else {
				return new ReturnData<Boolean>(false, 202, "error");
			}
		}else {
			Car car = Car.dao.findById(req.getId());
			if (car != null)
			{
				car.set(Car.TableField.CREATETIME, new Date());
				if (car.update()) {
					return new ReturnData<Boolean>(true, 200, "OK");
				} else {
					return new ReturnData<Boolean>(false, 202, "error");
				}
			}else {
				return new ReturnData<Boolean>(false, 203, "信息不存在");
			}
		}
	

	}

	public ReturnData<Boolean> delete(ReqCarDelete req) {
		if (req == null) {
			return new ReturnData<Boolean>(false, 202, "error");
		}
		Car car = Car.dao.findById(req.getId());
		if (car != null && car.delete()) {
			return new ReturnData<Boolean>(true, 200, "OK");
		} else {
			return new ReturnData<Boolean>(false, 202, "error");
		}

	}

	// //获取0最新列表-1找车列表-2找人列表-3我发布的列表
	public ReturnData<RspPage<Car>> queryPage(ReqCarPage req) {

		Page<Car> page = null;
		switch (req.getType()) {
		case 0:
			page = Car.dao.getNewPage(req.getNowPage(), req.getPageSize());
			break;
		case 1:
			// 车找人列表(1 车找人 0 人找车)
			page = Car.dao.getPage(req.getNowPage(), req.getPageSize(), 1);

			break;
		case 2:
			// 人找车列表 (1 车找人 0 人找车)
			page = Car.dao.getPage(req.getNowPage(), req.getPageSize(), 0);
			break;
		case 3:
			// 我发布的列表
			page = Car.dao.getMyPage(req.getNowPage(), req.getPageSize(), req
					.getFinalData().getMac());
			break;
		default:
			page = Car.dao.getNewPage(req.getNowPage(), req.getPageSize());
			break;
		}

		RspPage<Car> rspPage = new RspPage<>();
		if (page != null) {
			rspPage.setNowPage(req.getNowPage());
			rspPage.setTotalPage(page.getTotalPage());
			rspPage.setList(page.getList());
		} else {
			rspPage.setNowPage(req.getNowPage());
			rspPage.setTotalPage(0);
			rspPage.setList(new ArrayList<Car>());
		}
		return new ReturnData<RspPage<Car>>(rspPage, 200, "OK");
	}

	// 获取条件搜索列表
	public ReturnData<RspPage<Car>> querPage(ReqCarQPage req) {
		Page<Car> page = Car.dao.queryPage(req);

		RspPage<Car> rspPage = new RspPage<>();
		if (page != null) {
			rspPage.setNowPage(req.getNowPage());
			rspPage.setTotalPage(page.getTotalPage());
			rspPage.setList(page.getList());
		} else {
			rspPage.setNowPage(req.getNowPage());
			rspPage.setTotalPage(0);
			rspPage.setList(new ArrayList<Car>());
		}
		return new ReturnData<RspPage<Car>>(rspPage, 200, "OK");
	}

}
